package com.wallet.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.wallet.entity.Transaction;
import com.wallet.entity.example.TransactionExample;
import com.wallet.entity.result.ResultCode;
import com.wallet.service.BaseService;
import com.wallet.util.HttpClientUtil;

@RestController
@RequestMapping("/transaction")
public class TransactionController {
	
	@Autowired
	private BaseService<Transaction, TransactionExample> transactionService;
	
	@ResponseBody
	@RequestMapping(value = "/init")
	public int initarticle() {
		String result=HttpClientUtil.get("http://localhost:9527/dev-api/vue-element-admin/transaction/list");
		Map<String,Object> maps = (Map<String,Object>)JSON.parse(result);  
		Map<String,Object> data=(Map<String,Object>)maps.get("data");
		String items=data.get("items").toString();
		List<Transaction> Transaction=JSON.parseArray(items, Transaction.class);
		return transactionService.insertBatch(Transaction);
	}
	
	@ResponseBody
	@RequestMapping(value = "/list")
	public Map<String, Object> getList(){
		Map<String,Object> result=new HashMap<String, Object>();
		result.put("code", ResultCode.SUCCESS.getVal());
		Map<String, Object> data=new HashMap<String, Object>();
		data.put("total", transactionService.getCount());
		data.put("items",transactionService.selectAll());
		result.put("data",data);
		return result;
	}
}
